/**
 * 权限相关API
 * 对应后端RightsRouter.js中的路由
 */
import api, { extractData } from './axios';

/**
 * 权限接口定义
 */
export interface Right {
  id?: string | number;
  _id?: string;
  title: string;
  key: string;
  pagepermisson?: number;
  grade?: number;
  children?: Right[];
}

/**
 * 角色接口定义
 */
export interface Role {
  id?: string | number;
  _id?: string;
  roleName: string;
  roleType?: number;
  rights?: string[];
}

/**
 * 获取角色列表
 * 对应后端GET /adminapi/roles路由
 */
export const getRoleList = async () => {
  const response = await api.get('/roles');
  return extractData(response);
};

/**
 * 获取权限列表
 * 对应后端GET /adminapi/rights路由
 */
export const getRights = async (params?: Record<string, string | number | boolean>) => {
  const response = await api.get('/rights', { params });
  return extractData(response);
};

/**
 * 获取带有子权限的权限列表
 * 对应后端GET /adminapi/rights/children路由
 */
export const getRightsWithChildren = async () => {
  const response = await api.get('/rights/children');
  return extractData(response);
};

/**
 * 更新角色权限
 * 对应后端PATCH /adminapi/roles/:id/rights路由
 */
export const updateRoleRights = async (id: string | number, rights: string[]) => {
  const response = await api.patch(`/roles/${id}/rights`, { rights });
  return extractData(response);
};

/**
 * 删除角色
 * 对应后端DELETE /adminapi/roles/:id路由
 */
export const deleteRole = async (id: string | number) => {
  const response = await api.delete(`/roles/${id}`);
  return response.data;
};

/**
 * 更新权限状态
 * 对应后端PATCH /adminapi/rights/:id路由
 */
export const updateRightState = async (id: string | number, pagepermisson: number) => {
  const response = await api.patch(`/rights/${id}`, { pagepermisson });
  return extractData(response);
};

/**
 * 更新子权限状态
 * 对应后端PATCH /adminapi/rights/children/:id路由
 */
export const updateChildRightState = async (id: string | number, pagepermisson: number) => {
  const response = await api.patch(`/rights/children/${id}`, { pagepermisson });
  return extractData(response);
};

/**
 * 删除权限
 * 对应后端DELETE /adminapi/rights/:id路由
 */
export const deleteRight = async (id: string | number) => {
  const response = await api.delete(`/rights/${id}`);
  return response.data;
};

/**
 * 删除子权限
 * 对应后端DELETE /adminapi/rights/children/:id路由
 */
export const deleteChildRight = async (id: string | number) => {
  const response = await api.delete(`/rights/children/${id}`);
  return response.data;
};

/**
 * 根据ID获取权限详情
 * 对应后端GET /adminapi/rights/:id路由
 */
export const getRightById = async (id: string | number) => {
  const response = await api.get(`/rights/${id}`);
  return extractData(response);
};

/**
 * 根据ID获取子权限详情
 * 对应后端GET /adminapi/rights/children/:id路由
 */
export const getChildRightById = async (id: string | number) => {
  const response = await api.get(`/rights/children/${id}`);
  return extractData(response);
};

/**
 * 添加权限
 * 对应后端POST /adminapi/rights路由
 */
export const addRight = async (right: Partial<Right>) => {
  const response = await api.post('/rights', right);
  return extractData(response);
};

/**
 * 修改权限
 * 对应后端PUT /adminapi/rights/:id路由
 */
export const updateRight = async (id: string | number, right: Partial<Right>) => {
  // 确保data中不包含id和_id字段，避免MongoDB文档ID冲突
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const { id: _, _id: __, ...updateData } = right;
  
  const response = await api.put(`/rights/${id}`, updateData);
  return extractData(response);
};

/**
 * 添加子权限
 * 对应后端POST /adminapi/rights/children路由
 */
export const addChildRight = async (right: Partial<Right>) => {
  const response = await api.post('/rights/children', right);
  return extractData(response);
};

/**
 * 修改子权限
 * 对应后端PUT /adminapi/rights/children/:id路由
 */
export const updateChildRight = async (id: string | number, right: Partial<Right>) => {
  // 确保data中不包含id和_id字段，避免MongoDB文档ID冲突
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const { id: _, _id: __, ...updateData } = right;
  
  const response = await api.put(`/rights/children/${id}`, updateData);
  return extractData(response);
};

/**
 * 使用后端API检查权限
 * 对应后端POST /adminapi/permissions/check路由
 */
export const checkPermission = async (permissionKey: string) => {
  const response = await api.post('/permissions/check', { permissionKey });
  const result = extractData<{hasPermission: boolean}>(response);
  return result.hasPermission;
};

/**
 * 获取用户权限列表
 * 对应后端GET /adminapi/rights/user路由
 */
export const getUserRights = async () => {
  const response = await api.get('/rights/user');
  return extractData(response);
};

/**
 * 获取权限树结构
 * 对应后端GET /adminapi/rights/tree路由
 */
export const getRightsTree = async () => {
  const response = await api.get('/rights/tree');
  return extractData(response);
};

/**
 * 获取侧边栏菜单项
 * 对应后端GET /adminapi/sidebar路由
 */
export const getSidebarItems = async () => {
  const response = await api.get('/sidebar');
  return extractData(response);
};

export default {
  getRoleList,
  getRights,
  getRightsWithChildren,
  updateRoleRights,
  deleteRole,
  updateRightState,
  updateChildRightState,
  deleteRight,
  deleteChildRight,
  getRightById,
  getChildRightById,
  addRight,
  updateRight,
  addChildRight,
  updateChildRight,
  checkPermission,
  getUserRights,
  getRightsTree,
  getSidebarItems
}; 